﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XingFuStudio.Pioneer.Net;
using XingFuStudio.Pioneer.Data;
using XingFuStudio.Pioneer.Entity;
using XingFuStudio.Pioneer.BusinessLogic;
using XingFuStudio.Pioneer.Container.Interface.Event;
using XingFuStudio.Pioneer.Container;
using XingFuStudio.Pioneer.Applications.City;

namespace XingFuStudio.Pioneer.Applications.TestForm {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("欢迎使用Leisure开心网自动管理平台，此为测试窗口，不包含任何配置信息。");
            Console.WriteLine("");

            List<AccountInfo> accounts = new DbAccount().GetAccounts();

            if (accounts.Count == 0) {
                string result = "N";
                Console.WriteLine("系统检测到您为第一次运行，没有读取到任何用户信息。");
                Console.WriteLine("");
                while (result.ToLower() == "n") {
                    Console.Write("请输入开心网帐户：");
                    string email = Console.ReadLine();
                    Console.Write("请输入密码：");
                    string password = Console.ReadLine();
                    Console.Write("确认请输入“Y”，返回请输入“N”");
                    result = Console.ReadLine();

                    if (result.ToLower() == "y") {
                        AccountInfo account = new AccountInfo() {
                            Email = email, Password = new Password(password)
                        };

                        new DbAccount().CreateAccount(account);
                    }
                }

                if (result.ToLower() == "y") {
                    accounts = new DbAccount().GetAccounts();
                    Console.WriteLine("");
                }
            }

            HttpManager httpManager = new HttpManager();
            LoginManager login = new LoginManager(httpManager);
            login.PringLog += new EventHandler<LogEventArgs>(PringLog);

            foreach (AccountInfo account in accounts) {
                login.Login(account);
            }

            AppEntrance pluginManager = new AppEntrance();
            pluginManager.PrintLog += new EventHandler<LogEventArgs>(PringLog);
            pluginManager.Error += new EventHandler<ErrorEventArgs>(Error);

            pluginManager.Initialize("City");
            pluginManager.Run(accounts);

            Console.ReadLine();

        }

        static void Error(object sender, ErrorEventArgs e) {
            Console.WriteLine(e.Exception.Message);
        }

        static void PringLog(object sender, LogEventArgs e) {
            if (e.IsNewLing) {
                Console.WriteLine(e.LogMessage);
            } else {
                Console.Write(e.LogMessage + " ");
            }
        }
    }
}
